/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.examples;

import com.google.api.adwords.starterkit.StarterKit;
import com.google.api.adwords.starterkit.settings.AdGroupSettings;
import com.google.api.adwords.starterkit.settings.CampaignSettings;
import com.google.api.adwords.starterkit.settings.KeywordSettings;
import com.google.api.adwords.starterkit.settings.StarterKitSettings;
import com.google.api.adwords.starterkit.wrappers.AdGroupWrapper;
import com.google.api.adwords.starterkit.wrappers.CampaignWrapper;
import com.google.api.adwords.starterkit.wrappers.KeywordWrapper;
import com.google.api.adwords.v201101.cm.KeywordMatchType;

import au.com.bytecode.opencsv.CSVReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

/**
 * The Class AdParamExample is a demo that updates the AdParam values 
 * of EXISTING campaigns. To run this demo without any error, please, 
 * first run the ImportFromCSV demo to make sure that specific campaigns
 * will be created to be used by this demo
 *  
 * @author rodrigo.f.vale@gmail.com  (Rodrigo Vale)
 */
public class AdParamExample {
  public static void howToUse() {
    System.out.print(
        "SYNOPSIS\n" +
        "       AdParamExample [SETTINGS] [FILE]\n" +
        "\n" +
        "DESCRIPTION\n" +
        "       Updates the AdParam values of EXISTING campaigns. To run\n" +
        "this demo without any error, please, first run the\n" +
        "ImportFromCSV demo to make sure that specific campaigns\n" +
        "will be created to be used by this demo.\n" +
        "\n" +
        "EXAMPLE\n" +
        "       java starterkit.jar AdParamExample settings.xml " +
        "AdParam.csv\n");
    System.exit(0);
  }
  public static void main(String[] args) throws IOException {
    if (args.length < 2) {
      howToUse();
    }
    
    String settingsFile = args[0];
    String csvFile = args[1];
    String [] fields;

    StarterKitSettings starterKitSettings 
        = new StarterKitSettings(settingsFile);
    CampaignSettings campaignSettings 
        = starterKitSettings.getCampaignSettings();
    AdGroupSettings adGroupSettings = starterKitSettings.getAdGroupSettings();
    KeywordSettings keywordSettings = starterKitSettings.getKeywordSettings();
    
    StarterKit starterKit = new StarterKit(starterKitSettings);
    starterKit.load();

    // open the CSV file to be processed 
    CSVReader reader = null; 
    try {
      reader = new CSVReader(new FileReader(csvFile));
    } catch (FileNotFoundException e) {
      System.out.println("Error loading CSV file");
      System.exit(1);
    }
    
    // gets the header line and process it
    fields = reader.readNext();
    HashMap<String, Integer> header = new HashMap<String, Integer>();
    int count = 0;
    for (String field : fields) {
      header.put(field, count);
      count++;
    }
    
    // we don't want to modify the campaigns fields or create new ones
    // we just want to update AdParam value    
    campaignSettings.setCanModifyBudget(false);
    campaignSettings.setCanModifyStatus(false);
    campaignSettings.setAutoCreation(false);
    
    adGroupSettings.setBidModifiable(false);
    adGroupSettings.setStatusModifiable(false);
    adGroupSettings.setAutoCreation(false);
    
    keywordSettings.setCanModifyBid(false);
    keywordSettings.setCanModifyStatus(false);
    keywordSettings.setCanModifyUrl(false);
    keywordSettings.setAutoCreation(false);
    
    // process the CSV content
    while ((fields = reader.readNext()) != null) {
      String campaign = fields[header.get("Campaign")];
      String adGroup = fields[header.get("Ad Group")];
      String keyword = fields[header.get("Keyword")];
      String keywordType = fields[header.get("Keyword Type")].toUpperCase();
      String adParam1 = fields[header.get("AdParam1")];
      String adParam2 = fields[header.get("AdParam2")];
      
      if (keyword.length() > 0) {
        CampaignWrapper campaignWrapper = 
          starterKit.getCampaignWrapper(campaign, campaignSettings);
        AdGroupWrapper adGroupWrapper = 
          campaignWrapper.getAdGroupWrapper(adGroup, adGroupSettings);
        keywordSettings.setMatchType(keywordType);
        KeywordWrapper kwdWrapper = 
          adGroupWrapper.getKeywordWrapper(keyword, false,
          "http://www.google.com", keywordSettings);
        kwdWrapper.getKeywordParamWrapper(adParam1, adParam2);
      }
    }
    starterKit.save();
    starterKit.done();    
  }
}